<?php

namespace App\Admin\Forms;


use App\Imports\OrdersImport;
use Dcat\Admin\Widgets\Form;
use Maatwebsite\Excel\Facades\Excel;

class OrdersImportForm extends Form
{


    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

        set_time_limit(0);
        //上传文件位置，这里默认是在storage中，如有修改请对应替换
        $file = storage_path('/app/admin/' . $input['file']);
        Excel::import(new OrdersImport(), $file); # 导入
        return $this->response()->success('数据导入成功')->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->file('file', '上传数据（Excel）')->rules('required', ['required' => '文件不能为空']);
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'name'  => 'John Doe',
            'email' => 'John.Doe@gmail.com',
        ];
    }
}
